package com.ithailong.reconsitutionmassion.service.Impl;

import com.ithailong.reconsitutionmassion.common.Constants;
import com.ithailong.reconsitutionmassion.dao.Impl.UserDaoImpl;
import com.ithailong.reconsitutionmassion.entity.User;
import com.ithailong.reconsitutionmassion.exception.MyException;
import com.ithailong.reconsitutionmassion.exception.ServicesException;
import com.ithailong.reconsitutionmassion.service.UserService;
import com.ithailong.reconsitutionmassion.util.ConstantMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * author:hlc
 * date:2023/10/28
 *
 * @author 17930
 */
@Service
public class UserServiceImpl implements UserService {
   @Autowired
   private UserDaoImpl userDao;

   @Override
   public User userLogin(String username, String password, HttpSession session) throws ServicesException {
      User user = userDao.useLogin(username, password);

      if (user == null) {
         throw new ServicesException(Constants.CODE_401, "用户名或密码错误");
      }
      if ("0".equals(user.getStatus())) {
         throw new ServicesException(Constants.CODE_401, "该账号未激活请联系管理员激活");
      }
      session.setAttribute(ConstantMessage.LOGIN_USER, user);//将用户存储在session中
      return user;
   }

   @Override
   public int register(User user) {
      User selectUserByUsername = userDao.selectUserByUsername(user.getUsername());
      User selectUserByEmail = userDao.selectUserByEmail(user.getEmail());
      if (selectUserByUsername != null) {
         throw new ServicesException(Constants.CODE_401, "用户名已经注册");
      }
      if (selectUserByEmail != null) {
         throw new ServicesException(Constants.CODE_401, "该邮箱已经注册");
      }
      Date date = new Date();
      user.setCreateTime(date);
      user.setRole("user");
      user.setStatus("0");
      user.setName("kfm");
      return userDao.register(user);
   }

   /**
    * 查询所有用户
    *
    * @return
    */
   @Override
   public List<User> userList(int pageNum, int pageSize,User user) {
      return userDao.userList(pageNum, pageSize,user);
   }

   @Override
   public boolean deleteUser(int id, HttpSession session) throws MyException {
      User user = userDao.selectUserById(id);
      User attribute = (User) session.getAttribute(ConstantMessage.LOGIN_USER);
      if ("admin".equals(user.getRole()) && !"superAdmin".equals(attribute.getRole())) {
         throw new MyException("你无权删除该用户");
      }
      if ("superAdmin".equals(user.getRole())) {
         //拿到当前登录的用户

         attribute.setStatus("0");
         userDao.updateUser(user);
         throw new MyException("你正在尝试删除超级管理员，该账号将被锁定，解除请联系管理员");
      }
      boolean b = userDao.deleteUser(id);
      if (userDao.deleteUser(id)) {
         throw new MyException("删除失败");
      }
      return b;
   }

   @Override
   public User selectUserById(int id) {
      return userDao.selectUserById(id);
   }

   @Override
   public int updateUser(User user) {
      boolean b = userDao.updateUser(user);
      if (!b) {
         return 1;
      }
      return 0;
   }
}
